package mybatis;


import java.io.IOException;
import java.io.InputStream;
import java.util.Set;

import org.junit.Test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import com.qdlc.p2p.dal.mybatis.utils.ConfigurationableMybatisGenerator;


/**
 * 自动生成Mapper实体类
 * @author linya 2016-01-21
 */
public class MyBaitsGeneratorStartHelper {
	
	@Test
	public void start() {
		ConfigurationableMybatisGenerator generator = new ConfigurationableMybatisGenerator();
		InputStream is = null;
		try {
			ConfigurationParser cp = new ConfigurationParser(generator.getProperties(), null);
			is = MyBaitsGeneratorStartHelper.class.getResourceAsStream(ConfigurationableMybatisGenerator.CONFIG_FILE);
			Configuration config = cp.parseConfiguration(is);
			MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, new DefaultShellCallback(true), null);
			Set<String> fullyQualifiedTableNames = generator.getFullyQualifiedTableNames();
			myBatisGenerator.generate(null, null, fullyQualifiedTableNames);
		} catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				if(is != null){
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
}
